package com.freesky.exportpdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 16:48 2019-03-26
 * @description:
 */
public class TestPdf {
    private static Font fontChinese;// 中文、12、正常
    private static BaseFont bfChinese;

    static {
        try {
            // 中文字体(现在高版本的不支持中文包)
            bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            fontChinese = new Font(bfChinese, 12, Font.NORMAL);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String generatePDF() throws IOException, DocumentException {
        String fileDir = "";
        String fileName = "报名确认单1.pdf";
        String filePath = fileDir+"/"+fileName;
        Document document = new Document(PageSize.A4, 20, 20, 20, 20);
        FileOutputStream fos = new FileOutputStream(filePath);
        // 使用PDFWriter进行写文件操作
        PdfWriter.getInstance(document, fos);
        document.open();

        //标题
        PdfPTable tableTitle = new PdfPTable(1);
        tableTitle.setSpacingAfter(30f);
        tableTitle.setWidthPercentage(100);// 表格的宽度百分比
        tableTitle.getDefaultCell().setBorderWidth(0);// 边框宽度
        tableTitle.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        // 添加表头元素
        tableTitle.addCell(new Paragraph("盲人医疗按摩人员考试考生报名确认单", new Font(bfChinese, 20, Font.BOLD)));
        document.add(tableTitle);


        //内容
        int colNumber = 4;
        // 创建有4列的表格
        PdfPTable datatable = new PdfPTable(colNumber);
        datatable.setSpacingAfter(30f);
        // 定义表格的宽度
        int[] cellsWidth = {1, 2, 1, 2};
        // 单元格宽度
        datatable.setWidths(cellsWidth);
        // 表格的宽度百分比
        datatable.setWidthPercentage(100);

        datatable.getDefaultCell().setPadding(2);// 单元格的间隔
        datatable.getDefaultCell().setBorderWidth(1);// 边框宽度
        datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        datatable.addCell(getNomalCell("姓名：",null));
        datatable.addCell(getNomalCell("",null));
        datatable.addCell(getNomalCell("身份证号：",null));
        datatable.addCell(getNomalCell("",null));
        datatable.addCell(getNomalCell("备注：",null));

        PdfPCell remark = new PdfPCell(new Phrase("暂无", fontChinese));
        remark.setFixedHeight(30);
        remark.setVerticalAlignment(Element.ALIGN_MIDDLE);
        remark.setColspan(3);
        datatable.addCell(remark);
        document.add(datatable);// 把表格加入文档

        //签名
        PdfPTable qianMing = new PdfPTable(4);
        qianMing.setSpacingAfter(50f);
        int[] cellsQianMing = {5, 1, 1, 1};
        qianMing.setWidths(cellsQianMing);
        qianMing.setWidthPercentage(100);
        qianMing.getDefaultCell().setPadding(2);// 单元格的间隔
        qianMing.getDefaultCell().setBorderWidth(1);// 边框宽度
        qianMing.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);

        PdfPCell qianZiCell = new PdfPCell(new Paragraph("签字：", fontChinese));
        qianZiCell.setBorder(0);
        //设置列位置0左，1中，2右
        qianZiCell.setHorizontalAlignment(2);
        qianMing.addCell(qianZiCell);
        qianMing.addCell(getTableUnderLine());
        PdfPCell timeCell = new PdfPCell(new Paragraph("时间：", fontChinese));
        timeCell.setBorder(0);
        timeCell.setHorizontalAlignment(2);
        qianMing.addCell(timeCell);
        qianMing.addCell(getTableUnderLine());
        document.add(qianMing);

        Paragraph p1 = new Paragraph("报名受理地点：报考人员户籍所在地人员户籍所在地或从业医疗机构所在地的设区的市级残疾人联合会", new Font(bfChinese, 11, Font.NORMAL));
        Paragraph p2 = new Paragraph("报名及初审时间：2019年5月3日—2019年6月14日。", new Font(bfChinese, 11, Font.NORMAL));
        Paragraph p3 = new Paragraph("报名复核时间：2019年6月15日—2019年7月10日。", new Font(bfChinese, 11, Font.NORMAL));
        document.add(p1);
        document.add(p2);
        document.add(p3);
        document.close();
        return filePath;
    }
    public static void main(String[] args) throws DocumentException, IOException {
        generatePDF();
    }

    static PdfPCell getTableUnderLine() throws IOException, DocumentException {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        PdfPCell underLine = new PdfPCell(new Paragraph("", new Font(bfChinese, 12, Font.UNDERLINE)));
        underLine.setHorizontalAlignment(Element.ALIGN_CENTER);
        underLine.setVerticalAlignment(Element.ALIGN_MIDDLE);
        underLine.setBorderWidthBottom(1);
        underLine.setBorderWidthLeft(0);
        underLine.setBorderWidthRight(0);
        underLine.setBorderWidthTop(0);
        return underLine;
    }

    static PdfPCell getNomalCell(String content, Integer position) {
        PdfPCell cell = new PdfPCell(new Phrase(content, fontChinese));
        cell.setFixedHeight(30);
        if (position != null) {
            cell.setHorizontalAlignment(position);

        }
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }
}
